﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Text;
using System.Web.SessionState;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.App;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// M_CommitteeMember 的摘要说明
    /// </summary>
    public class M_CommitteeMember : IHttpHandler, IRequiresSessionState
    {
        tblCPerson_CommMemberBLL commBLL = new tblCPerson_CommMemberBLL();
        app_tblCPerson_CommMemberBLL app_commBLL = new app_tblCPerson_CommMemberBLL();
        int CMUID = 0;
        int TypeUID = 0;
        int CommPositionUID = 0;
        int cid = 0;
        int CustomerUID = 0;
        DateTime startTime = new DateTime();
        DateTime entTime = new DateTime();
        public void ProcessRequest(HttpContext context)
        {
            string strRet = string.Empty;
            context.Response.ContentType = "text/plain";
            var customerENT = BasePage.GetInstance().GetCustomerFromSession();
            if (customerENT != null)
            {
                string optionType = context.Request.QueryString["OptionType"];
                    if (optionType == "get")
                    {
                        strRet = GetCommENT(context);
                    }
                    else if (optionType == "update")
                    {
                        strRet = UpdateCommENT(context);
                    }
                    else if (optionType == "add")
                    {
                        strRet = AddCommENT(context);
                    }
                    else if (optionType == "del")
                    {
                        strRet = DeleteCommENT(context);
                    }
            }
            else
            {
                strRet = ResourceManage.GetResourceString("msg200016");
            }
            context.Response.Write(strRet);
            context.Response.Flush();
        }

        private string GetCommENT(HttpContext context)
        {
            string strRet = string.Empty;
            string strCMUID = context.Request.QueryString["CMUID"];
            if (!string.IsNullOrEmpty(strCMUID))
            {
                string[] strTemp = strCMUID.Split('_');
                int KeyValue = Convert.ToInt32(strTemp[0]);
                string strFrom = strTemp[1];
                if (strFrom == "app")
                {
                    app_tblCPerson_CommMember app_commENT = new app_tblCPerson_CommMember();
                    app_tblCPerson_CommMemberBLL app_commBLL = new app_tblCPerson_CommMemberBLL();
                    app_commENT.AppCperson_CommMemberUID = KeyValue;
                    app_commENT.Deleted = 0;
                    app_commENT = app_commBLL.GetENTByENT(app_commENT);
                    if (app_commENT != null && app_commENT.Deleted == 0)
                    {
                        strRet = GetStrFromApp(app_commENT);
                    }
                    else
                    {
                        strRet = ResourceManage.GetResourceString("msg200014");
                    }
                }
                else
                {
                    tblCPerson_CommMember commENT = new tblCPerson_CommMember();
                    tblCPerson_CommMemberBLL commBLL = new tblCPerson_CommMemberBLL();
                    commENT.Cperson_CommMemberUID = KeyValue;
                    commENT.Deleted = 0;
                    commENT = commBLL.GetEntity(commENT);
                    if (commENT != null)
                    { strRet = GetStrFromTbl(commENT); }

                    else
                    { strRet = ResourceManage.GetResourceString("msg200014"); }
                }
            }
            return strRet;
        }

        private string GetStrFromApp(app_tblCPerson_CommMember app_commENT)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("{\"TypeValue\":\"");
            strBuilder.Append(app_commENT.CommTypeUID);
            strBuilder.Append("\",\"Position\":\"");
            strBuilder.Append(app_commENT.CommPositionUID);
            strBuilder.Append("\",\"txtStartDate\":\"");
            if (app_commENT.StartDate != null)
            {
                strBuilder.Append(BasePage.GetInstance().DateFormat(app_commENT.StartDate));
            }
            strBuilder.Append("\",\"txtEndDate\":\"");
            if (app_commENT.EndDate != null)
            {
                strBuilder.Append(BasePage.GetInstance().DateFormat(app_commENT.EndDate));
            }
            strBuilder.Append("\",\"txtYear\":\"");
            strBuilder.Append(app_commENT.Year);
            strBuilder.Append("\",\"txtRmark\":\"");
            strBuilder.Append(app_commENT.Remarks);
            strBuilder.Append("\"}");
            return strBuilder.ToString();
        }
        private string GetStrFromTbl(tblCPerson_CommMember app_commENT)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("{\"TypeValue\":\"");
            strBuilder.Append(app_commENT.CommTypeUID);
            strBuilder.Append("\",\"Position\":\"");
            strBuilder.Append(app_commENT.CommPositionUID);
            strBuilder.Append("\",\"txtStartDate\":\"");
            if (app_commENT.StartDate != null)
            {
                strBuilder.Append(BasePage.GetInstance().DateFormat(app_commENT.StartDate));
            }
            strBuilder.Append("\",\"txtEndDate\":\"");
            if (app_commENT.EndDate != null)
            {
                strBuilder.Append(BasePage.GetInstance().DateFormat(app_commENT.EndDate));
            }
            strBuilder.Append("\",\"txtYear\":\"");
            strBuilder.Append(app_commENT.Year);
            strBuilder.Append("\",\"txtRmark\":\"");
            strBuilder.Append(app_commENT.Remarks);
            strBuilder.Append("\"}");
            return strBuilder.ToString();
        }

        private string UpdateCommENT(HttpContext context)
        {
            string strRet = string.Empty;
            string strCMUID = context.Request.QueryString["CMUID"];
            int TypeValue=Convert.ToInt32(context.Request.QueryString["TypeValue"]);
            int Position=Convert.ToInt32(context.Request.QueryString["Position"]);
            DateTime txtStartDate= Convert.ToDateTime(context.Request.QueryString["txtStartDate"]);
            DateTime txtEndDate=Convert.ToDateTime(context.Request.QueryString["txtEndDate"]);
            string txtYear=context.Request.QueryString["txtYear"];
            string txtRmark=context.Request.QueryString["txtRmark"];
            int AppCPUID=0;
            int TblCPUID=0;
            int.TryParse(context.Request.QueryString["TblCPUID"],out TblCPUID);
            int.TryParse(context.Request.QueryString["AppCPUID"],out AppCPUID);

            if (!string.IsNullOrEmpty(strCMUID))
            {
                string[] strTemp = strCMUID.Split('_');
                int KeyValue = Convert.ToInt32(strTemp[0]);
                string strFrom = strTemp[1];
                if (strFrom == "app")
                {
                    app_tblCPerson_CommMember app_commENT = new app_tblCPerson_CommMember();
                    app_commENT.AppCperson_CommMemberUID = KeyValue;
                    app_commENT.Deleted = 0;
                    app_commENT = app_commBLL.GetENTByENT(app_commENT);
                    if (app_commENT != null && app_commENT.Deleted == 0)
                    {
                        if (AppCPUID > 0)
                            app_commENT.AppContactPersonUID = AppCPUID;
                        app_commENT.CommTypeUID = TypeValue;
                        app_commENT.CommPositionUID = Position;
                        app_commENT.StartDate = txtStartDate;
                        app_commENT.EndDate = txtEndDate;
                        app_commENT.Year = txtYear;
                        app_commENT.Remarks = txtRmark;
                        if (app_commBLL.UpdateENT(app_commENT) > 0)
                        {
                            strRet = ResourceManage.GetResourceString("msg200004");
                        }
                        else
                        { strRet = ResourceManage.GetResourceString("msg200003"); }
                    }
                    else { strRet = ResourceManage.GetResourceString("msg200014"); }
                }
                else {
                    tblCPerson_CommMember commENT = new tblCPerson_CommMember();
                    commENT.Cperson_CommMemberUID=KeyValue;
                    commENT.Deleted = 0;
                    commENT = commBLL.GetEntity(commENT);
                    if (commENT != null)
                    {
                        app_tblCPerson_CommMember app_commENT = new app_tblCPerson_CommMember();
                        app_commENT.Cperson_CommMemberUID = KeyValue;
                        app_commENT.Deleted = 0;
                        app_commENT = app_commBLL.GetENTByENT(app_commENT);
                        if (app_commENT != null)
                        {
                            if (app_commENT.Deleted == 0)
                            {
                                if (AppCPUID > 0)
                                    app_commENT.AppContactPersonUID = AppCPUID;
                                app_commENT.CommTypeUID = TypeValue;
                                app_commENT.CommPositionUID = Position;
                                app_commENT.StartDate = txtStartDate;
                                app_commENT.EndDate = txtEndDate;
                                app_commENT.Year = txtYear;
                                app_commENT.Remarks = txtRmark;
                                if (app_commBLL.UpdateENT(app_commENT) > 0)
                                {strRet = ResourceManage.GetResourceString("msg200004");}
                                else
                                { strRet = ResourceManage.GetResourceString("msg200003"); }
                            }
                            else { strRet = ResourceManage.GetResourceString("msg200014"); }
                        }
                        else { //插入到APP表 
                            app_tblCPerson_CommMember app_ENT = new app_tblCPerson_CommMember();
                            if (AppCPUID > 0)
                                app_ENT.AppContactPersonUID = AppCPUID;
                            app_ENT.Cperson_CommMemberUID = commENT.Cperson_CommMemberUID;
                            app_ENT.ContactPersonUID = commENT.ContactPersonUID;
                            app_ENT.CommTypeUID = TypeValue;
                            app_ENT.CommPositionUID = Position;
                            app_ENT.StartDate = txtStartDate;
                            app_ENT.EndDate = txtEndDate;
                            app_ENT.Year = txtYear;
                            app_ENT.Remarks = txtRmark;
                            if (app_commBLL.InsertENT(app_ENT) > 0)
                            { strRet = ResourceManage.GetResourceString("msg200004"); }
                            else
                            { strRet = ResourceManage.GetResourceString("msg200003"); }
                        }
                    }
                    else { strRet = ResourceManage.GetResourceString("msg200014"); }
                }
            }
            return strRet; 
        }

        private string AddCommENT(HttpContext context)
        {
            string str = string.Empty;
            int AppCPUID = 0;
            int TblCPUID = 0;
            int.TryParse(context.Request.QueryString["TblCPUID"], out TblCPUID);
            int.TryParse(context.Request.QueryString["AppCPUID"], out AppCPUID);
            int TypeValue = Convert.ToInt32(context.Request.QueryString["TypeValue"]);
            int Position = Convert.ToInt32(context.Request.QueryString["Position"]);
            DateTime txtStartDate = Convert.ToDateTime(context.Request.QueryString["txtStartDate"]);
            DateTime txtEndDate = Convert.ToDateTime(context.Request.QueryString["txtEndDate"]);
            string txtYear = context.Request.QueryString["txtYear"];
            string txtRmark = context.Request.QueryString["txtRmark"];

            app_tblCPerson_CommMember app_ENT = new app_tblCPerson_CommMember();
            if (AppCPUID > 0)
                app_ENT.AppContactPersonUID = AppCPUID;
            if (TblCPUID > 0)
                app_ENT.ContactPersonUID = TblCPUID;
            app_ENT.CommTypeUID = TypeValue;
            app_ENT.CommPositionUID = Position;
            app_ENT.StartDate = txtStartDate;
            app_ENT.EndDate = txtEndDate;
            app_ENT.Year = txtYear;
            app_ENT.Remarks = txtRmark;
            if (app_commBLL.InsertENT(app_ENT) > 0)
            { str = ResourceManage.GetResourceString("msg200004"); }
            else
            { str = ResourceManage.GetResourceString("msg200003"); }
            return str;
        }
        private string DeleteCommENT(HttpContext context)
        {
            string strRet = string.Empty;
            string strCMUID = context.Request.QueryString["CMUID"];
            int TypeValue=Convert.ToInt32(context.Request.QueryString["TypeValue"]);
            int Position=Convert.ToInt32(context.Request.QueryString["Position"]);
            DateTime txtStartDate= Convert.ToDateTime(context.Request.QueryString["txtStartDate"]);
            DateTime txtEndDate=Convert.ToDateTime(context.Request.QueryString["txtEndDate"]);
            string txtYear=context.Request.QueryString["txtYear"];
            string txtRmark=context.Request.QueryString["txtRmark"];
            int AppCPUID=0;
            int TblCPUID=0;
            int.TryParse(context.Request.QueryString["TblCPUID"],out TblCPUID);
            int.TryParse(context.Request.QueryString["AppCPUID"],out AppCPUID);

            if (!string.IsNullOrEmpty(strCMUID))
            {
                string[] strTemp = strCMUID.Split('-');
                int KeyValue = Convert.ToInt32(strTemp[0]);
                string strFrom = strTemp[1];
                if (strFrom == "app")
                {
                    app_tblCPerson_CommMember ent = new app_tblCPerson_CommMember();
                    ent.AppCperson_CommMemberUID = KeyValue;
                    ent = app_commBLL.GetENTByENT(ent);
                    if (ent != null)
                    {
                        if (ent.Cperson_CommMemberUID > 0)
                        {
                            ent.Deleted = 1;
                            if (app_commBLL.UpdateENT(ent) > 0)
                                strRet = ResourceManage.GetResourceString("msg200011");
                            else
                                strRet = ResourceManage.GetResourceString("msg200010");
                        }
                        else
                        {
                            if (app_commBLL.DeleteENT(ent) > 0)
                                strRet = ResourceManage.GetResourceString("msg200011");
                            else
                                strRet = ResourceManage.GetResourceString("msg200010");

                        }
                    }
                    else { strRet = ResourceManage.GetResourceString("msg200014"); }
                }
                else {
                    app_tblCPerson_CommMember ent = new app_tblCPerson_CommMember();
                    ent.Cperson_CommMemberUID = KeyValue;
                    ent = app_commBLL.GetENTByENT(ent);
                    if (ent != null)
                    {
                        if (ent.Deleted == 1)
                            strRet = ResourceManage.GetResourceString("msg200014");
                        else
                        {
                            ent.Deleted = 1;
                            app_commBLL.UpdateENT(ent);
                        }
                    }
                    else
                    {
                        app_tblCPerson_CommMember app_ENT = new app_tblCPerson_CommMember();
                        if (AppCPUID > 0)
                            app_ENT.AppContactPersonUID = AppCPUID;
                        if (TblCPUID > 0)
                            app_ENT.ContactPersonUID = TblCPUID;
                        app_ENT.Cperson_CommMemberUID = KeyValue;
                        app_ENT.Deleted = 1;
                        app_ENT.CommTypeUID = TypeValue;
                        app_ENT.CommPositionUID = Position;
                        app_ENT.StartDate = txtStartDate;
                        app_ENT.EndDate = txtEndDate;
                        app_ENT.Year = txtYear;
                        app_ENT.Remarks = txtRmark;
                        if (app_commBLL.InsertENT(app_ENT) > 0)
                        { strRet = ResourceManage.GetResourceString("msg200004"); }
                        else
                        { strRet = ResourceManage.GetResourceString("msg200003"); }
                    }
                }
            }
            return strRet;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}